package com.shj.em.biz.warehouse.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.biz.Constants;
import com.shj.em.biz.sell.entity.Order;
import com.shj.em.biz.sell.mapper.OrderMapper;
import com.shj.em.biz.warehouse.entity.Shipment;
import com.shj.em.biz.warehouse.mapper.ShipmentMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.plugins.Page;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * 订单出货记录 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-12-10
 */
@Service
@Transactional
public class ShipmentServiceImpl extends ServiceImpl<ShipmentMapper, Shipment> implements ShipmentService {

    @Autowired
    ShipmentMapper shipmentMapper;

    @Autowired
    OrderMapper orderMapper;

    public Page<Shipment> queryShipmentPage(Map<String, Object> params){
        int offset = (Integer)params.get("offset");
        int pageSize = (Integer)params.get("limit");
        int pageNo = offset / pageSize + 1;
        Page<Shipment> page = new Page(pageNo, pageSize);
        List<Shipment> list = shipmentMapper.queryShipmentPage(page, params);
        return page.setRecords(list);
    }

    public List<Shipment> queryShipmentByOrderId(Long orderId){
        Wrapper<Shipment> wrapper = new EntityWrapper<>();
        wrapper.eq("order_id", orderId);
        return shipmentMapper.selectList(wrapper);
    }

    public void insertShipment(Shipment shipment){
        shipmentMapper.insert(shipment);
        Order order = orderMapper.selectById(shipment.getOrderId());
        order.setShipmentStatus(Constants.SHIPMENT_STATUS_PARTIAL);
        orderMapper.updateById(order);
    }

    /*public void deleteShipments(List<Long> ids){
        shipmentMapper.deleteBatchIds(ids);
        Wrapper<Shipment> wrapper = new EntityWrapper<>();
        wrapper.eq()
    }*/
}
